import scrapy
from pyquery import PyQuery
from dangdang.items import DangdangItem


class DangSpider(scrapy.Spider):
    name = 'dang'
    allowed_domains = ['search.dangdang.com']
    start_urls = ['http://search.dangdang.com/?key=python&act=input&page_index=1']

    def parse(self, response):
        mypy=PyQuery(response.text)
        titleList=mypy("ul.bigimg > li > a:first-of-type").items()
        imgList=mypy("ul.bigimg > li > a >img").items()
        authorList=mypy("p.search_book_author > span:first-of-type > a:first-of-type").items()
        publishList=mypy("p.search_book_author  span:nth-of-type(3) a").items()
        priceList=mypy("p.price span.search_now_price").items()

        for i in range(60):
            items=DangdangItem()

            items['title']=next(titleList).attr('title')
            items['pic']=next(imgList).attr('data-original')
            items['author']=next(authorList).attr('title')
            items['publish']=next(publishList).text()
            items['price']=next(priceList).text()
            yield items

            # print(next(titleList).attr('title'))
            # print(next(imgList).attr('data-original'))
            # print(next(authorList).attr('title'))
            # print(next(publishList).text())
            # print(next(priceList).text())


